###########################################
# Table 2: Main results

###########################################

# Define dependent and independent variables
dep <- "perc_yes ~"
main <- "post:itm_prop_change +"
Xs <- paste("presidential_el","midterm_el","odd_year","turnout","Bond","failed_recently",sep = "+")
Xs_elFE <-  paste("turnout","Bond","failed_recently",sep = "+")
Xs_bond <-  paste("turnout","log(bond_ph)","failed_recently",sep = "+")
FEs <- "|LEAID + election_FE|0"
cluster <- "|LEAID "

# Restrict to school district with more than 1 referendums (cannot identify with school district fixed effects)
dfreg <- df[df$one_election ==0,]

# Restrict to close elections as defined in Cellini et al. (2010)
dfreg25 <- dfreg[dfreg$tight_election ==1,]


# No election FE nor interaction term
R0 <- felm(data = dfreg,
           weights = dfreg$population_votingage,
           formula = as.formula(paste(dep,"post +" ,Xs,"|LEAID |0",cluster,sep = "")))

# No election fixed effects
R1 <- felm(data = dfreg,
           weights = dfreg$population_votingage,
           formula = as.formula(paste(dep,"post + post:itm_prop_change +",Xs,"|LEAID |0",cluster,sep = "")))

# baseline
R2 <- felm(data = dfreg,
           weights = dfreg$population_votingage,
           formula = as.formula(paste(dep,main,Xs_elFE,FEs,cluster,sep = "")))

# baseline with closed election only
R3 <- felm(data = dfreg25,
           weights = dfreg25$population_votingage,
           formula = as.formula(paste(dep,main,Xs_elFE,FEs,cluster,sep = "")))

# Bond only with control for bond amount
R4 <- felm(data = dfreg25[dfreg25$type == "Bond"  ,],
           weights = dfreg25[dfreg25$type == "Bond" ,]$population_votingage,
           formula = as.formula(paste(dep,main,Xs_bond,FEs,cluster,sep = "")))

# Add triple interaction with SALT wasted
R5 <- felm(data = dfreg25,
           weights = dfreg25$population_votingage,
           formula = as.formula(paste(dep,main,"wasted_salt_perc:post:itm_prop_change +",Xs_elFE,FEs,cluster,sep = "")))

# Add triple interaction with high schocked
R6 <- felm(data = dfreg25,
           weights = dfreg25$population_votingage,
           formula = as.formula(paste(dep,main,"highpropchange:itm_prop_change:post +",Xs_elFE,FEs,cluster,sep = "")))



# Output regressions
stargazer(R0,R1,R2,R3,R4,R5,R6,
          type = "latex",
          omit.stat = c("ser","f"),
          digits = 2,
          omit.table.layout = "n",
          omit = c("failed_recently","Bond","presidential_el","midterm_el","odd_year","turnout"),
          out = "Output/Table2.main_results.tex",
          title = "Average Causal Response Estimates",
          label = "tab:refdum_mainresults",
          dep.var.labels.include =   FALSE)



rm(list=setdiff(ls(), c("df","city")))
